//
//  ViewController.swift
//  EmoticonKeyBoard
//
//  Created by 周先生 on 15/11/21.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    private lazy var keyBoardView: EmoticonKeyBoardView = EmoticonKeyBoardView { [weak self] (em) -> () in
        self?.insertText(em)
        print(em)
    
    }
    
    /**
     处理点击事件
     
     :param: emoticon
     */
    private func insertText(emoticon: Emotion) {
    
        //空表情
        if emoticon.isEmpty {
            print("空表情")
            return
        
        }
        //删除
        if emoticon.isRemove {
           textView.deleteBackward()
        }
        //emoji表情
        if emoticon.code != nil {
            textView.replaceRange(textView.selectedTextRange!, withText: emoticon.emojiStr ?? "")
        
        }
    }
    @IBOutlet weak var textView: UITextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        //成为第一响应者
        textView.becomeFirstResponder()
        textView.inputView = keyBoardView
    }

    func demo() {
        //替换键盘的输入视图
        let v = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
        v.backgroundColor = UIColor.blueColor()
        textView.inputAccessoryView = v
    }

}

